package com.newhero.product.module.sys.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.newhero.product.common.controller.BaseController;
import com.newhero.product.common.util.BeanUtils;
import com.newhero.product.common.util.DigestUtils;
import com.newhero.product.common.util.StringUtils;
import com.newhero.product.module.sys.model.User;
import com.newhero.product.module.sys.service.IUserService;
/**
 * @ClassName: PersonController 
 * @Description: 个人资料、修改密码
 * @author Caolj 648198687@qq.com 
 * @date 2017-3-27 上午9:08:31
 */
@Controller
@RequestMapping("/admin")
public class PersonController extends BaseController {

	@Autowired
	private IUserService userServiceImpl;

	@RequiresPermissions("/admin/myInfo") 
	@RequestMapping(value = "/myInfo", method = RequestMethod.GET)
	public String myInfo(HttpServletRequest request,
			HttpServletResponse response) {
		User user = userServiceImpl.selectById(getUserID());
		request.setAttribute("user", user);
		return "sys/user/myInfo";
	}

	@RequestMapping(value = "/myInfoEdit", method = RequestMethod.POST)
	@ResponseBody
	public void myInfoEdit(HttpServletRequest request,
			HttpServletResponse response) {
		User user = userServiceImpl.selectById(getUserID());
		super.renderSuccess(user);
	}

	@RequestMapping(value = "/myInfoSave", method = RequestMethod.POST)
	@ResponseBody
	public void myInfoSave(User user, HttpServletRequest request,
			HttpServletResponse response, ModelMap model) {
		try {
			if (!StringUtils.isBlank(user.getUserID())) {
				User su = userServiceImpl.selectById(user.getUserID());
				// BeanUtils.copyNotNullProperties(user, su);
				BeanUtils.copy(user, su);
				userServiceImpl.updateById(su);
			}
			renderSuccess();
		} catch (Exception e) {
			logger.error(e);
			renderError("更新失败");
			e.printStackTrace();
		}

	}

	@RequiresPermissions("/admin/mypasswd") 
	@RequestMapping(value = "/mypasswd", method = RequestMethod.GET)
	public String mypasswd(HttpServletRequest request,
			HttpServletResponse response) {
		request.setAttribute("userID", getUserID());
		return "sys/user/mypasswd";
	}

	@RequestMapping(value = "/myPassWdSave", method = RequestMethod.POST)
	@ResponseBody
	public Object myPasswordSave(
			@RequestParam(value = "oldpwd", required = true) String oldpwd,
			@RequestParam(value = "newpwd", required = true) String newpwd,
			@RequestParam(value = "userID", required = true) String userID,
			HttpServletRequest request, HttpServletResponse response,
			ModelMap model) {
			if (userID != null) {
				User su = userServiceImpl.selectById(userID);
				oldpwd = DigestUtils.md5Hex(oldpwd);
				if (oldpwd.equals(su.getUserPwd())) {
					userServiceImpl.updatePwdByUserId(userID,
							DigestUtils.md5Hex(newpwd));
					return renderSuccess("密码修改成功！");
				} else {
					return renderError("旧密码不正确！");
				}
			}else{
				return renderError("旧密码不正确！");
			}
		
	}
}
